package io.renren.modules.substation.service.impl;

import io.renren.modules.substation.entity.TInfoUserAreaEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.substation.dao.TInfoRoleAreaDao;
import io.renren.modules.substation.entity.TInfoRoleAreaEntity;
import io.renren.modules.substation.service.TInfoRoleAreaService;


@Service("tInfoRoleAreaService")
public class TInfoRoleAreaServiceImpl extends ServiceImpl<TInfoRoleAreaDao, TInfoRoleAreaEntity> implements TInfoRoleAreaService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TInfoRoleAreaEntity> page = this.page(
                new Query<TInfoRoleAreaEntity>().getPage(params),
                new QueryWrapper<TInfoRoleAreaEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveRoleArea(Long roleId, List<Long> roleAreaList, Long createId) {
        //先删除角色与区域关系
        deleteBatch(new Long[]{roleId});

        if(roleAreaList.size() == 0){
            return ;
        }

        //保存角色与区域关系
        for(Long areaId : roleAreaList){
            TInfoRoleAreaEntity tInfoRoleAreaEntity = new TInfoRoleAreaEntity();
            if (areaId < 0L){//设备ID
                tInfoRoleAreaEntity.setRoleArea(0L - areaId);
                tInfoRoleAreaEntity.setAreaType("1");
            } else {//区域
                tInfoRoleAreaEntity.setRoleArea(areaId);
                tInfoRoleAreaEntity.setAreaType("0");
            }
            tInfoRoleAreaEntity.setRoleId(roleId);
            tInfoRoleAreaEntity.setCreateOpr(createId);
            tInfoRoleAreaEntity.setCreateTime(new Date());
            this.save(tInfoRoleAreaEntity);
        }
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

    @Override
    public List<TInfoRoleAreaEntity> listByRoleId(Long id) {
        return baseMapper.listByRoleId(id);
    }

    @Override
    public List<TInfoRoleAreaEntity> getAreaPermsByUser(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        return baseMapper.getAreaPermsByUser(params);
    }

    @Override
    public List<Long> getAreaListByUser(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        return baseMapper.getAreaListByUser(params);
    }

}